using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;
namespace Data
{
    public class ChiTietMatDoSuDungPhongRepository
    {



		//////////////////////////////////////////////////////////

		                                                          

		//            Enter code your here                      //

		                                                          

		//////////////////////////////////////////////////////////

        public static ChiTietMatDoSuDungPhongDTO CreateChiTietMatDoSuDungPhongDTOFromChiTietMatDoSuDungPhongEF(ChiTietMatDoSuDungPhong objEf) // EF -> DTO
        {
            ChiTietMatDoSuDungPhongDTO objDTO = new ChiTietMatDoSuDungPhongDTO();
            Mapper.CreateMap<ChiTietMatDoSuDungPhong, ChiTietMatDoSuDungPhongDTO>();
            objDTO = Mapper.Map<ChiTietMatDoSuDungPhong, ChiTietMatDoSuDungPhongDTO>(objEf);
            return objDTO;
        }

        public static ChiTietMatDoSuDungPhong CreateChiTietMatDoSuDungPhongEFFromChiTietMatDoSuDungPhongDTO(ChiTietMatDoSuDungPhongDTO objDTO)    //DTO -> EF
        {
            ChiTietMatDoSuDungPhong objEF = new ChiTietMatDoSuDungPhong();
            Mapper.CreateMap<ChiTietMatDoSuDungPhongDTO, ChiTietMatDoSuDungPhong>();
            objEF = Mapper.Map<ChiTietMatDoSuDungPhongDTO, ChiTietMatDoSuDungPhong>(objDTO);
            return objEF;
        }

       public static int Count()  //Dem so luong cac bo du lieu trong bang
        {
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            int qr = (from x in Context.ChiTietMatDoSuDungPhongList
                   select x.MaChiTietMatDoPhong).Count();
            return qr;
        }

        public int GetIdentity() // tra ve gia tri ma moi them vo( gia tri Ma lon nhat trong Bang).
        {
            try
            {
                 QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
                  var query = from x in Context.ChiTietMatDoSuDungPhongList
                        orderby x.MaChiTietMatDoPhong descending
                        select x.MaChiTietMatDoPhong;
                  var Ma = query.First();
                  return int.Parse(Ma.ToString());
            }
            catch
            {
                return 0;
            }
        }

        public bool Them(ChiTietMatDoSuDungPhongDTO objDTO)
        {
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                ChiTietMatDoSuDungPhong objEF = CreateChiTietMatDoSuDungPhongEFFromChiTietMatDoSuDungPhongDTO(objDTO);
                context.AddToChiTietMatDoSuDungPhongList(objEF);
                context.SaveChanges();
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

        public void CapNhat(ChiTietMatDoSuDungPhongDTO objDTO)
        {
            try
            {
                ChiTietMatDoSuDungPhong objEF = CreateChiTietMatDoSuDungPhongEFFromChiTietMatDoSuDungPhongDTO(objDTO);
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                context.ChiTietMatDoSuDungPhongList.ApplyCurrentValues(objEF);
                context.SaveChanges();
            }
            catch (System.Exception )
            {            	
            }
        }

        public List<ChiTietMatDoSuDungPhongDTO> LayDanhSach(int Start, int Record)//lay Record bo du lieu tu vi tri Start
        { 
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            var query = (from x in Context.ChiTietMatDoSuDungPhongList
                        orderby x.MaChiTietMatDoPhong
                        select x).Skip(Start - 1).Take(Record);
            List<ChiTietMatDoSuDungPhongDTO> lst = new List<ChiTietMatDoSuDungPhongDTO>();
            foreach (var obj in query)
            {
                ChiTietMatDoSuDungPhongDTO lp = ChiTietMatDoSuDungPhongRepository.CreateChiTietMatDoSuDungPhongDTOFromChiTietMatDoSuDungPhongEF(obj);
                lst.Add(lp);
            }
            return lst;
        }

        public List<ChiTietMatDoSuDungPhongDTO> LayDanhSach()//lay tat ca danh sach
        {
            List<ChiTietMatDoSuDungPhongDTO> lst = new List<ChiTietMatDoSuDungPhongDTO>();
            QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
            var query = from ChiTietMatDoSuDungPhong in context.ChiTietMatDoSuDungPhongList
                        select ChiTietMatDoSuDungPhong;
            foreach (var objEf in query)
            {
                ChiTietMatDoSuDungPhongDTO objDTO = CreateChiTietMatDoSuDungPhongDTOFromChiTietMatDoSuDungPhongEF(objEf);
                lst.Add(objDTO);
            }
            return lst;
        }

        public ChiTietMatDoSuDungPhongDTO LayTheoMa(int MaChiTietMatDoSuDungPhong)
        {
            ChiTietMatDoSuDungPhongDTO objDTO = new ChiTietMatDoSuDungPhongDTO();
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                var query = from ChiTietMatDoSuDungPhong in context.ChiTietMatDoSuDungPhongList
                            where ChiTietMatDoSuDungPhong.MaChiTietMatDoPhong == MaChiTietMatDoSuDungPhong
                            select ChiTietMatDoSuDungPhong;
                ChiTietMatDoSuDungPhong objEF = query.First();
                objDTO = CreateChiTietMatDoSuDungPhongDTOFromChiTietMatDoSuDungPhongEF(objEF);
            }
            catch (System.Exception )
            {
            }
            return objDTO;
        }

//
//Khoa ngoai MatDoSuDungPhong
//
        public List<ChiTietMatDoSuDungPhongDTO> LayDanhSachTheoMaMatDoSuDungPhong( int MaMatDoSuDungPhong)
        {
            List<ChiTietMatDoSuDungPhongDTO> lst = new List<ChiTietMatDoSuDungPhongDTO>();
            try
            {
                 QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
                 var query = from ChiTietMatDoSuDungPhong in context.ChiTietMatDoSuDungPhongList
                         where ChiTietMatDoSuDungPhong.MaMatDoPhong == MaMatDoSuDungPhong 
                        select ChiTietMatDoSuDungPhong;
                 foreach (var objEf in query)
                 {
                      ChiTietMatDoSuDungPhongDTO objDTO = CreateChiTietMatDoSuDungPhongDTOFromChiTietMatDoSuDungPhongEF(objEf);
                       lst.Add(objDTO);
                 }
            }
            catch
            {}
            return lst;
        }



        public void XoaTheoMaMatDoSuDungPhong( int MaMatDoSuDungPhong, bool XoaKhoaNgoai=false)
        {
            List<ChiTietMatDoSuDungPhongDTO> lst = this.LayDanhSachTheoMaMatDoSuDungPhong( MaMatDoSuDungPhong);
            for( int i = 0; i < lst.Count; i++ )
            {
                 int iMa = lst[i].MaChiTietMatDoPhong;
                 this.Xoa(iMa, XoaKhoaNgoai);
            }
        }


//
//Khoa ngoai Phong
//
        public List<ChiTietMatDoSuDungPhongDTO> LayDanhSachTheoMaPhong( int MaPhong)
        {
            List<ChiTietMatDoSuDungPhongDTO> lst = new List<ChiTietMatDoSuDungPhongDTO>();
            try
            {
                 QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
                 var query = from ChiTietMatDoSuDungPhong in context.ChiTietMatDoSuDungPhongList
                         where ChiTietMatDoSuDungPhong.MaPhong == MaPhong 
                        select ChiTietMatDoSuDungPhong;
                 foreach (var objEf in query)
                 {
                      ChiTietMatDoSuDungPhongDTO objDTO = CreateChiTietMatDoSuDungPhongDTOFromChiTietMatDoSuDungPhongEF(objEf);
                       lst.Add(objDTO);
                 }
            }
            catch
            {}
            return lst;
        }

        public void XoaTheoMaPhong( int MaPhong, bool XoaKhoaNgoai=false)
        {
            List<ChiTietMatDoSuDungPhongDTO> lst = this.LayDanhSachTheoMaPhong( MaPhong);
            for( int i = 0; i < lst.Count; i++ )
            {
                 int iMa = lst[i].MaChiTietMatDoPhong;
                 this.Xoa(iMa, XoaKhoaNgoai);
            }
        }

        public bool Xoa(int MaChiTietMatDoSuDungPhong, bool XoaKhoaNgoai=false)
        {
            try
            {
                if( XoaKhoaNgoai == true)
                {                
                }
                QuanLyKhachSanEntities context = new QuanLyKhachSanEntities();
                var query = from x in context.ChiTietMatDoSuDungPhongList
                            where x.MaChiTietMatDoPhong == MaChiTietMatDoSuDungPhong 
                            select x;
                if(query.First()!= null)
                {
                    context.DeleteObject(query.First());
                    context.SaveChanges();
                }
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

////////////////////////////////////////////////////////////////

    }
}

